package com.gimi.cloud.jimi.client.service.userInfo.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gimi.cloud.jimi.client.service.userInfo.LbsDistrictService;
import com.gimi.cloud.jimi.client.service.userInfo.LbsLocationService;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.commonService.util.APIHttpClientUtil;
import com.gimi.cloud.jimi.dao.dao.userInfo.ApiConfigDao;
import com.gimi.cloud.jimi.dao.dao.userInfo.LbsLocationDao;
import com.gimi.cloud.jimi.entity.core.ApiConfig;
import com.gimi.cloud.jimi.entity.userInfo.LbsDistrict;
import com.gimi.cloud.jimi.entity.userInfo.LbsLocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

@Service
public class LbsLocationServiceImpl implements LbsLocationService {
	private static Logger log = LoggerFactory.getLogger(LbsLocationServiceImpl.class);
	@Autowired
	private LbsLocationDao lbsLocationDao;
	@Autowired
	private LbsDistrictService lbsDistrictService;
	@Autowired
	private ApiConfigDao apiConfigDao;

	@Override
	public void insertLbsLocation(double longitude, double latitude) {
		try{ //整体try catch 避免影响其他事务
			List<LbsLocation> lbsLocations = lbsLocationDao.getLbsLoactions(longitude,latitude);
			if(null == lbsLocations || lbsLocations.isEmpty()){
				LbsLocation lbsLocation = new LbsLocation();
				lbsLocation.setLatitude(latitude);
				lbsLocation.setLongitude(longitude);
				ApiConfig apiConfig = apiConfigDao.getApiConfigByKey("lbs_location");
				Map<String,String> params = new HashMap<String,String>();
				params.put(":latitude", ""+latitude);
				params.put(":longitude", ""+longitude);
				Map<String, Object> resultStr = APIHttpClientUtil.sendGet(apiConfig, params, null, false);
				String result =""+resultStr.get("resultStr");
				if(isJson(result)){
					JSONObject jsonObj = JSON.parseObject(result);
					if(jsonObj.containsKey("result")){
						jsonObj = jsonObj.getJSONObject("result");
						if(jsonObj.containsKey("addressComponent")){
							jsonObj = jsonObj.getJSONObject("addressComponent");
							String country = ""+jsonObj.get("country");
							String province = ""+jsonObj.get("province");
							String city = ""+jsonObj.get("city");
							String district = ""+jsonObj.get("district");
							String street = ""+jsonObj.get("street");
							Map<String, Long> disTricts = lbsDistrictService.getDistrictByHashCode(new Integer[]{country.hashCode(),province.hashCode(),city.hashCode(),district.hashCode(),street.hashCode()});
							disTricts= null ==disTricts?new HashMap<String,Long>():disTricts;
							if(!disTricts.containsKey(""+country.hashCode())){
								LbsDistrict lbsDistrict = new LbsDistrict();
								lbsDistrict.setHashcode(country.hashCode());
								lbsDistrict.setVal(country);
								lbsDistrictService.addLbsDistrict(lbsDistrict);
								disTricts.put(""+lbsDistrict.getHashcode(), lbsDistrict.getId());
							}
							if(!disTricts.containsKey(""+province.hashCode())){
								LbsDistrict lbsDistrict = new LbsDistrict();
								lbsDistrict.setHashcode(province.hashCode());
								lbsDistrict.setVal(province);
								lbsDistrictService.addLbsDistrict(lbsDistrict);
								disTricts.put(""+lbsDistrict.getHashcode(), lbsDistrict.getId());
							}
							if(!disTricts.containsKey(""+city.hashCode())){
								LbsDistrict lbsDistrict = new LbsDistrict();
								lbsDistrict.setHashcode(city.hashCode());
								lbsDistrict.setVal(city);
								lbsDistrictService.addLbsDistrict(lbsDistrict);
								disTricts.put(""+lbsDistrict.getHashcode(), lbsDistrict.getId());
							}
							if(!disTricts.containsKey(""+district.hashCode())){
								LbsDistrict lbsDistrict = new LbsDistrict();
								lbsDistrict.setHashcode(district.hashCode());
								lbsDistrict.setVal(district);
								lbsDistrictService.addLbsDistrict(lbsDistrict);
								disTricts.put(""+lbsDistrict.getHashcode(), lbsDistrict.getId());
							}
							if(!disTricts.containsKey(""+street.hashCode())){
								LbsDistrict lbsDistrict = new LbsDistrict();
								lbsDistrict.setHashcode(street.hashCode());
								lbsDistrict.setVal(street);
								lbsDistrictService.addLbsDistrict(lbsDistrict);
								disTricts.put(""+lbsDistrict.getHashcode(), lbsDistrict.getId());
							}
							lbsLocation.setFcountry(country.hashCode());
							lbsLocation.setFcity(city.hashCode());
							lbsLocation.setFdistrict(district.hashCode());
							lbsLocation.setFprovince(province.hashCode());
							lbsLocation.setFstreet(street.hashCode());
							String lbss = ""+lbsLocation.getFcountry()+lbsLocation.getFcity()+lbsLocation.getFdistrict()+lbsLocation.getFprovince()+lbsLocation.getFstreet();
							lbsLocation.setHashcode(lbss.hashCode());
							lbsLocationDao.insertSelective(lbsLocation);
						}
					}
				}
			}
		}catch(Exception e){
			log.error(e.getMessage(),e);
		}
	}
	
	/**
	 * 判断指定字符串是否是JSON类型
	 * @param str 待判断的字符串
	 * @return 判断结果
	 */
	public static boolean isJson(String str){
		try{
			if(StringUtil.isNotBlank(str)){
				JSON.parse(str);
				return true;
			}
		}catch(Exception e){}
		
		return false;
	}
}
